from crawler.base import BaseCrawler
import requests
from .jsl_login import get_cookie_str  # 导入获取 cookie 的函数
from storage.mysql_storage import DbHandler  # 假设存在一个获取数据库实例的函数
from crawler.jsl.jsl_data_map import alive_bond_map
import pandas as pd
from typing import Optional
from bs4 import BeautifulSoup

db_instance = DbHandler()
db_instance.create_session()

class Spider_jsl_bond_concept_parser(BaseCrawler):
    def __init__(self, name,url, data_process_type,params):
        super().__init__(name,url,data_process_type, params)
        self.db_instance = db_instance  # 获取数据库实例

    def fetch_one(self,item):
        bond_id = item['转债代码']
        """获取原始数据"""
        cookie_str = get_cookie_str(self.db_instance)  # 获取 cookie
        if not cookie_str:
            print("Failed to get valid cookie.")
            return bond_id,None
        headers = {
            "Cookie": cookie_str,
            "Init": "1",
            "Content-Type": "application/json; charset=utf-8",
        }
        response = requests.get(self.url+bond_id, params=self.params, headers=headers)
        if response.status_code == 200:
            return bond_id,response.text
        else:
            print(f"Failed to fetch data. Status code: {response.status_code}")
            return bond_id,None

    def parse(self, raw_data):
        """解析原始数据"""
        soup = BeautifulSoup(raw_data, 'html.parser')
        # 找到包含债券代码的元素
        bond_nm_div = soup.find('div', class_='bond_nm')
        # 提取债券代码
        if bond_nm_div:
            bond_code = bond_nm_div.text.strip().split()[-1]
        else:
            print("未找到包含债券代码的元素。")
        # 找到概念标签所在的容器
        concept_container = soup.find('div', class_='concept')
        concept_list = concept_container.find('div', class_='list')
        # 提取所有的概念标签文本
        concept_tags = [a.text for a in concept_list.find_all('a')]
        concept_list = []
        for tag in concept_tags:
            concept_list.append(tag)
        pd_concept = pd.DataFrame({
            'bond_id': [bond_code],
            'concept_list': [concept_list]}, dtype=object)
        return pd_concept
